
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CreateBubble : MonoBehaviour
{
	private  float rightEdgeX 		= 13;

	public 	GameObject 	  			 blueBubbleInitialObject;
	public 	float      					 blueBubbleDelayTimeToCreate;
	public 	float      	  				 blueBubbleTimeCount;
	public 	List<GameObject>	 listBlueBubble = new List<GameObject> ();
	
	public 	GameObject 	  			 greenBubbleInitialObject;
	public 	float      	  				 greenBubbleDelayTimeToCreate;
	public 	float      	  				 greenBubbleTimeCount;
	public 	List<GameObject>	 listGreenBubble = new List<GameObject> ();

	public 	GameObject 	  			 redBubbleInitialObject;
	public 	float      	  				 redBubbleDelayTimeToCreate;
	public 	float      	  				 redBubbleTimeCount;
	public 	List<GameObject>	 listRedBubble = new List<GameObject> ();

	public 	GameObject 	  			 blackBubbleInitialObject;
	public 	float      	  				 blackBubbleDelayTimeToCreate;
	public 	float      	  				 blackBubbleTimeCount;
	public 	List<GameObject>	 listBlackBubble = new List<GameObject> ();

	// Use this for initialization
	void Start (){
		blueBubbleDelayTimeToCreate = 3f;
		blueBubbleTimeCount = 0f;

		greenBubbleDelayTimeToCreate = 2.5f;
		greenBubbleTimeCount = 0f; 

		redBubbleDelayTimeToCreate = 2.5f;
		redBubbleTimeCount = 0f; 

		blackBubbleDelayTimeToCreate = 2.5f;
		blackBubbleTimeCount = 0f; 
	}
	IEnumerator RemoveNullFromList(List<GameObject> list) {
		yield return new WaitForSeconds(5f);
		list.RemoveAll (item => item == null);
		
	}
	// Update is called once per frame
	void Update (){
		createBubble();
	}
	
	void createBubble(){
		createBlueBubble();
		createGreenBubble();
		createBlackBubble();
		createRedBubble();
	}
	
	
	void createBlueBubble(){		
		float compare = Time.time - blueBubbleTimeCount;
		if(compare > blueBubbleDelayTimeToCreate){
			blueBubbleTimeCount = Time.time;
			listBlueBubble.Add(Instantiate(blueBubbleInitialObject,new Vector2(rightEdgeX,random_y_offset()),Quaternion.identity) as GameObject);
			listBlueBubble[listBlueBubble.Count-1].name = "blueBubble"+listBlueBubble.Count;
		}
	}
	void createBlackBubble(){		
		float compare = Time.time - blackBubbleTimeCount;
		if(compare > blackBubbleDelayTimeToCreate){
			blackBubbleTimeCount = Time.time;
			listBlackBubble.Add(Instantiate(blackBubbleInitialObject,new Vector2(rightEdgeX,random_y_offset()),Quaternion.identity) as GameObject);
			listBlackBubble[listBlackBubble.Count-1].name = "blackBubble"+listBlackBubble.Count;
		}
	}
	void createRedBubble(){		
		float compare = Time.time - redBubbleTimeCount;
		if(compare > redBubbleDelayTimeToCreate){
			redBubbleTimeCount = Time.time;
			listRedBubble.Add(Instantiate(redBubbleInitialObject,new Vector2(rightEdgeX,random_y_offset()),Quaternion.identity) as GameObject);
			listRedBubble[listRedBubble.Count-1].name = "redBubble"+listRedBubble.Count;
		}
	}
	void createGreenBubble(){
		float compare = Time.time - greenBubbleTimeCount;
		if(compare > greenBubbleDelayTimeToCreate){
			greenBubbleTimeCount = Time.time;
			listBlueBubble.Add(Instantiate(greenBubbleInitialObject,new Vector2(rightEdgeX, random_y_offset()),Quaternion.identity) as GameObject);
			listBlueBubble[listBlueBubble.Count - 1].name = "greenBubble"+listBlueBubble.Count;
		}
	}
	
	float random_y_offset(){
		return Random.Range (-3.5f, 3.5f);
	}
}